package com.atguigu.gmall.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.model.product.BaseCategory2;
import com.atguigu.gmall.model.product.BaseCategory3;
import com.atguigu.gmall.service.ManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
@Api(tags = "商品管理接口")
//@CrossOrigin
public class ManageController {

    @Autowired
    private ManagerService managerService;

    @GetMapping("/getCategory1")
    @ApiOperation(value = "获取一级分类列表", notes = "获取所有一级分类信息")
    public Result getCategory1(){
      List<BaseCategory1> baseCategory1List = managerService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    @GetMapping("/getCategory2/{category1Id}")
    @ApiOperation(value = "根据一级分类ID获取二级分类列表", notes = "通过一级分类ID获取其下的所有二级分类")
    public Result getCategory2(@ApiParam(name = "category1Id", value = "一级分类ID", required = true) @PathVariable Long category1Id){
        //  调用服务层方法。
        List<BaseCategory2> baseCategory2List = managerService.getCategory2(category1Id);
        //  返回数据
        return Result.ok(baseCategory2List);
    }

    //  /admin/product/getCategory3/{category2Id}
    //  根据二级分类Id 查询三级分类数据
    @GetMapping("/getCategory3/{category2Id}")
    @ApiOperation(value = "根据二级分类ID获取三级分类列表", notes = "通过二级分类ID获取其下的所有三级分类")
    public Result getCategory3(@ApiParam(name = "category2Id", value = "二级分类ID", required = true) @PathVariable Long category2Id){
        //  调用服务层方法。
        List<BaseCategory3> baseCategory3List = managerService.getCategory3(category2Id);
        //  返回数据
        return Result.ok(baseCategory3List);
    }


    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    @ApiOperation(value = "获取属性信息列表", notes = "根据三级分类ID获取属性信息列表")
    public Result getAttrInfoList(@ApiParam(name = "category1Id", value = "一级分类ID", required = true) @PathVariable Long category1Id,
                                  @ApiParam(name = "category2Id", value = "二级分类ID", required = true) @PathVariable Long category2Id,
                                  @ApiParam(name = "category3Id", value = "三级分类ID", required = true) @PathVariable Long category3Id){

        //  调用服务层方法
        List<BaseAttrInfo> baseAttrInfoList = managerService.getAttrInfoList(category1Id,category2Id,category3Id);
        //  返回数据
        return Result.ok(baseAttrInfoList);
    }


    @PostMapping("saveAttrInfo")
    @ApiOperation(value = "保存平台属性", notes = "保存平台属性")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        //  调用服务层方法
        this.managerService.saveAttrInfo(baseAttrInfo);
        //  返回数据
        return Result.ok();
    }

    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //  调用服务层方法
        //        List<BaseAttrValue> baseAttrValueList = this.manageService.getAttrValueList(attrId);
        //        return Result.ok(baseAttrValueList);
        //  attrId 先查看平台属性 -- 如果有平台属性，那么从平台属性中获取到平台属性值集合.
        BaseAttrInfo baseAttrInfo = this.managerService.getBaseAttrInfo(attrId);
        //  返回数据.
        return Result.ok(baseAttrInfo.getAttrValueList());
    }

}